跳到主要内容

图片生成 API

请求方法

POST

请求地址

打开控制台,进入 我的推理服务,找到对应的推理服务,复制下图所示位置的请求地址: image

文生图CURL 示例

在上述复制的url后增加要请求的模式,如果是文生图,则增加 &kind=txt2img,实际使用时,请务必对应替换上图中所复制的url地址


curl --location '{填入所启动的推理服务的地址,如上图所示,删除花括号}&kind=txt2img' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '
{
"sd_model_checkpoint": "realisticVisionV51_v40VAE.safetensors",
"prompt": "A girl",
"negative_prompt": "",
"width": 1200,
"height": 673,
"sampler_name": "DPM++ 2M",
"scheduler": "Karras",
"steps": 20,
"cfg_scale": 7,
"batch_size": 1,
"clip_skip": 2,
"seed": -1,
"override_settings": {"sd_model_checkpoint": "realisticVisionV51_v40VAE.safetensors"}
}'

如果是图生图,则增加 &kind=img2img,格式如下所示,其中:encoded_image为图片的base64编码数据;如果要附加放大的功能,则开启enable_hr;如果要开启controlnet,则在alwayson_scripts参数中增加controlnet字段;如果要开启adetailer,则在alwayson_scripts参数中增加ADetailer字段;如果要开启Dynamic Thresholding (CFG Scale Fix),则在alwayson_scripts参数中增加相应字段;如果要叠加lora模型,则直接在prompt中增加<lora:lora_name:1>,第一个冒号后字符串为要添加的lora模型名称(不带后缀),第二个冒号后的字符串为lora模型的权重;如果要用inpaint功能,则在mask字段中增加蒙版图片的base64编码数据。


curl --location '{填入所启动的推理服务的地址,如上图所示,删除花括号}&kind=img2img' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '
{
"sd_model_checkpoint": "majicmixRealistic_v7.safetensors",
"prompt": "1 yellow cat,<lora:chinese-art:1>",
"negative_prompt": "",
"init_images": [encoded_image],
"inpainting_fill": 1,
"inpaint_full_res": true,
"inpaint_full_res_padding": 32,
"inpainting_mask_invert": 0,
"mask_blur": 4,
"mask": encoded_mask,
"subseed": -1,
"subseed_strength": 0,
"sampler_name": "DPM++ 2M",
"scheduler": "Karras",
"batch_size": 1,
"cfg_scale": 7,
"denoising_strength": 0.4,
"do_not_save_grid": false,
"do_not_save_samples": false,
"n_iter": 1,
"steps": 30,
"tiling": false,
"image_cfg_scale": 1.5,
"initial_noise_multiplier": 1,
"resize_mode": 0,
"restore_faces": false,
"s_churn": 0,
"s_min_uncond": 0,
"s_noise": 1,
"s_tmax": 0,
"s_tmin": 0,
"override_settings": {
"sd_model_checkpoint": "majicmixRealistic_v7.safetensors"
},
"enable_hr": true,
"eta": 0,
"firstphase_height": 0,
"firstphase_width": 0,
"height": 512,
"width": 512,
"script_args": [],
"script_name": "",
"alwayson_scripts":
{
"controlnet":
{
"args":
[
{
"control_mode": "Balanced",
"enabled": true,
"guidance_end": 1,
"guidance_start": 0,
"input_image": encoded_image,
"lowvram": false,
"model": "control_v11p_sd15_lineart [43d4be0d]",
"module": "lineart_realistic",
"pixel_perfect": true,
"processor_res": 0,
"threshold_a": 0,
"threshold_b": 0,
"weight": 1
}
]
},
"ADetailer": {
"args": [
{
"ad_model": "face_yolov8n.pt"
}
]
}
,
"Dynamic Thresholding (CFG Scale Fix)": {
"args": [true, 7, 100, "Half Cosine Up", 5, "Half Cosine Up", 5, 4.0]
}
},
"override_settings_restore_afterwards": true,
"disable_extra_networks": false,
"comments": {},
"seed": -1,
"seed_resize_from_h": -1,
"seed_resize_from_w": -1,
"send_images": true,
"save_images": false
}'

如果要利用extras里的图片高清化,则增加 &kind=extra-single-image,格式如下所示,其中encoded_image是图片的base64编码数据。


curl --location '{填入所启动的推理服务的地址,如上图所示,删除花括号}&kind=extra-single-image' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '
{
"resize_mode": 0,
"show_extras_results": true,
"gfpgan_visibility": 0,
"codeformer_visibility": 0,
"codeformer_weight": 0,
"upscaling_resize": 2,
"upscaling_resize_w": 512,
"upscaling_resize_h": 512,
"upscaling_crop": true,
"upscaler_1": "Lanczos",
"upscaler_2": "None",
"extras_upscaler_2_visibility": 0,
"image": encoded_image
}'

相关查询的重要接口,请求示例如下所示。

查询支持哪些checkpoints模型:


curl --location 'https://api.lanrui.co/sdapi/v1/sd-models?{填入截图中的endpoint地址,在本例中为endpoint=24596c72-56cf-4c-gic,删除花括号}' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '{}'

查询支持哪些vae模型:


curl --location 'https://api.lanrui.co/sdapi/v1/sd-vae?{填入截图中的endpoint地址,在本例中为endpoint=24596c72-56cf-4c-gic,删除花括号}' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '{}'

查询支持哪些schedulers:


curl --location 'https://api.lanrui.co/sdapi/v1/schedulers?{填入截图中的endpoint地址,在本例中为endpoint=24596c72-56cf-4c-gic,删除花括号}' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '{}'

查询支持哪些samplers:


curl --location 'https://api.lanrui.co/sdapi/v1/samplers?{填入截图中的endpoint地址,在本例中为endpoint=24596c72-56cf-4c-gic,删除花括号}' \
--header 'Authorization: Bearer {填入API Key,删除花括号}' \
--header 'Content-Type: application/json' \
--data '{}'

返回响应(json)

参数名参数类型参数释义
request_idstring返回的请求 id,根据任务 ID 请求接口获取结果